﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class AccessData
    {
        SqlConnection Cnn;
        SqlCommand _sqlCmd;

        public AccessData()
        {
            Cnn = new SqlConnection();
            Cnn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();


            _sqlCmd = new SqlCommand();
            _sqlCmd.Connection = Cnn;
        }
        protected SqlCommand sqlCmd
        {
            get { return _sqlCmd; }
        }

        public DataTable GetTable(string sql)
        {
            SqlConnection con = Cnn;
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            con.Close();
            return dt;
        }
        //Ham thuc thi cau lenh query
        public void ExcuteNonQuery(string sql)
        {
            SqlConnection con = Cnn;
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
            cmd.Dispose();
        }
        //Ham thuc thi cau lenh ExcuteScalar tra ve 1 gia tri
        public string ExcuteScalar(string sql)
        {
            SqlConnection con = Cnn;
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            string result = cmd.ExecuteScalar().ToString();
            con.Close();
            cmd.Dispose();
            return result;
        }
        public SqlDataReader ExcuteReader(string sql)
        {
            SqlConnection con = Cnn;
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
    }
}
